package com.jsnkj.basic.mvvm

import android.graphics.Color
import android.graphics.Point
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.DialogFragment

/**
 * 具备视图数据绑定的DialogFragment，背景透明
 * @param layoutId xml布局文件
 * @param type 0，垂直居中的dialog；1，宽度match_parent的底部dialog
 */
open class BaseMVVMDialogFragment<VDB : ViewDataBinding>(private var layoutId: Int, private var type: Int = 0) : DialogFragment() {

    lateinit var mVDB : VDB // 视图数据绑定

    override fun onStart() {
        super.onStart()
        dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mVDB = DataBindingUtil.inflate(inflater, layoutId, container, false)
        return mVDB.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        if (1 == type) {// 宽度match_parent，位于屏幕底部
            val point = Point()
            requireActivity().windowManager.defaultDisplay.getSize(point)
            dialog?.window?.setGravity(Gravity.BOTTOM)
            dialog?.window?.attributes?.width = point.x
        }
    }
}